//
//  MineShouRuViewController.m
//  LoveDraw
//
//  Created by 零零壹 on 2017/9/23.
//  Copyright © 2017年 梁晨. All rights reserved.
//

#import "MineShouRuViewController.h"
#import "KeBiaoTongjiTableViewCell.h"
#import "DatePickerView.h"
#import "NSDate+Extension.h"
@interface MineShouRuViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger page;
    NSString *year;
    NSString *month;
}
@property(nonatomic,strong)UITableView *mineListV;
//@property(nonatomic,strong)NSMutableArray *listArr;
@property(nonatomic,strong)NSDateComponents* comp;
@property(nonatomic,strong)NSDictionary *dataDic;
@property(nonatomic,strong)UIButton *custumBTN;
@end

@implementation MineShouRuViewController


-(UIButton *)custumBTN
{
    if (!_custumBTN)
    {
        _custumBTN = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _custumBTN.titleLabel.font = [UIFont systemFontOfSize:17 weight:UIFontWeightThin];

        [_custumBTN setTitle:[NSString stringWithFormat:@"%@年%@月",year,month] forState:(UIControlStateNormal)];
        [_custumBTN setImage:[UIImage imageNamed:@"down_arrow"] forState:(UIControlStateNormal)];
        [_custumBTN setTitleEdgeInsets:UIEdgeInsetsMake(0, -50, 0, 0)];
        [_custumBTN setImageEdgeInsets:UIEdgeInsetsMake(0, 120, 0, 0)];

        //        _custumBTN.titleLabel.font = [UIFont systemFontOfSize:20];
        [_custumBTN setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        _custumBTN.frame = CGRectMake(0, 0, 180, 60);
        [_custumBTN addTarget:self action:@selector(changDateAct) forControlEvents:(UIControlEventTouchUpInside)];

    }
    return _custumBTN;
}

-(UITableView *)mineListV
{
    if (!_mineListV) {
        _mineListV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KscreenW, KscreenH-64) style:(UITableViewStyleGrouped)];
        _mineListV.delegate = self;
        _mineListV.dataSource = self;
        _mineListV.estimatedSectionHeaderHeight = 0;
        _mineListV.estimatedSectionFooterHeight = 0;
        [_mineListV registerNib:[UINib nibWithNibName:@"KeBiaoTongjiTableViewCell" bundle:nil] forCellReuseIdentifier:@"KeBiaoTongjiTableViewCell"];
        _mineListV.separatorStyle = 0;
    }
    return _mineListV;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self getDate];

    [self.view addSubview:self.mineListV];
    [self Request];
//    /index.php?g=App&m=TeacherIndex&a=statTeacher

}

-(void)getDate
{
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    // 获取当前日期
    NSDate* dt = [NSDate date];
    // 定义一个时间字段的旗标，指定将会获取指定年、月、日、时、分、秒的信息
    unsigned unitFlags = NSCalendarUnitYear |
    NSCalendarUnitMonth |  NSCalendarUnitDay |
    NSCalendarUnitHour |  NSCalendarUnitMinute |
    NSCalendarUnitSecond | NSCalendarUnitWeekday;
    // 获取不同时间字段的信息
    self.comp = [gregorian components: unitFlags
                             fromDate:dt];

    year = [NSString stringWithFormat:@"%02ld",self.comp.year];
    month = [NSString stringWithFormat:@"%02ld",self.comp.month];
    self.navigationItem.titleView = self.custumBTN;
}



-(void)changDateAct
{
    DatePickerView *datepicker = [[DatePickerView alloc]initWithCompleteBlock:^(NSDate *selectDate) {
        [self.custumBTN setTitle:[NSString stringWithFormat:@"%@年%@月",year,month] forState:(UIControlStateNormal)];

        year = [selectDate stringWithFormat:@"yyyy"];
        month = [selectDate stringWithFormat:@"MM"];
        [self.custumBTN setTitle:[NSString stringWithFormat:@"%@年%@月",year,month] forState:(UIControlStateNormal)];

        [self Request];
    }];
    datepicker.themeColor = [UIColor blueColor];
    datepicker.datePickerStyle = DateStyleShowYearMonth;
    datepicker.dateType     = DateTypeStartDate;
    datepicker.title        = @"选择年月";
    datepicker.themeColor   = UIColors(MainColor);
    datepicker.minLimitDate = [NSDate date:@"2017-01-01" WithFormat:@"yyyy-MM-dd"];
    datepicker.maxLimitDate = [NSDate date:@"2025-01-01" WithFormat:@"yyyy-MM-dd"];
    [datepicker show];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *dataArr = self.dataDic[@"data_list"];

    if (dataArr.count == 0) {
        self.mineListV.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"nodata"]];
    }else
    {
        self.mineListV.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@""]];
    }
    return dataArr.count;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    [self.navigationController setNavigationBarHidden:NO animated:YES];
//    self.navigationItem.title = @"我的收入";
}


-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    NSArray *dataArr = self.dataDic[@"data_list"];
    if (dataArr.count == 0) {
        return nil;
    }
    UIView *headV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KscreenW, 70)];
    
    headV.backgroundColor = UIColors(0xEFEFEF);
    UILabel *titleL = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, (KscreenW-2)/3, 70)];
    titleL.backgroundColor = [UIColor whiteColor];
    titleL.textAlignment = 1;
    titleL.text = @"合计";
    titleL.textColor = UIColors(0x666666);
    
    
    UILabel *numL = [[UILabel alloc]init];
    numL.textColor = UIColors(MainColor);
    numL.textAlignment = 1;
    numL.backgroundColor = [UIColor whiteColor];
    numL.text = [NSString stringWithFormat:@"%@",self.dataDic[@"total_nums"]];
    
    
    UILabel *money = [[UILabel alloc]initWithFrame:CGRectMake((KscreenW-2)*2/3+2, 0, (KscreenW-2)/3, 70)];
    money.text = [NSString stringWithFormat:@"%@元",self.dataDic[@"total_fee"]];
    money.textColor = UIColors(MainColor);
    money.backgroundColor = [UIColor whiteColor];
    money.textAlignment = 1;
    
    titleL.font = [UIFont systemFontOfSize:14];
    money.font = [UIFont systemFontOfSize:14];
    numL.font = [UIFont systemFontOfSize:14];
    
    [headV addSubview:titleL];
    [headV addSubview:money];
    [headV addSubview:numL];
    
    [numL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleL.mas_right).offset(1);
        make.top.equalTo(headV.mas_top);
        make.bottom.equalTo(headV.mas_bottom);
        make.right.equalTo(money.mas_left).offset(-1);
    }];
    return headV;
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    NSArray *dataArr = self.dataDic[@"data_list"];
    if (dataArr.count == 0) {
        return FLT_MIN;
    }else
    {
        return 70;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    NSArray *dataArr = self.dataDic[@"data_list"];
    if (dataArr.count == 0) {
        return FLT_MIN;
    }else
    {
        return 70;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSArray *dataArr = self.dataDic[@"data_list"];
    if (dataArr.count == 0) {
        return nil;
    }
    UIView *headV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KscreenW, 70)];
    headV.backgroundColor = UIColors(0xEFEFEF);
    UILabel *titleL = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, (KscreenW-2)/3, 70)];
    titleL.backgroundColor = [UIColor whiteColor];
    titleL.textAlignment = 1;
    titleL.text = @"班级";
    titleL.textColor = UIColors(0x666666);

    UILabel *num = [[UILabel alloc]initWithFrame:CGRectMake((KscreenW-2)/3+1, 0, (KscreenW-2)/3, 70)];
    num.textColor = UIColors(0x666666);
    num.textAlignment = 1;
    num.backgroundColor = [UIColor whiteColor];
    num.text = @"课次";

    UILabel *money = [[UILabel alloc]initWithFrame:CGRectMake((KscreenW-2)*2/3+2, 0, (KscreenW-2)/3, 70)];
    money.text = @"课费";
    money.textColor = UIColors(0x666666);
    money.backgroundColor = [UIColor whiteColor];
    money.textAlignment = 1;

    titleL.font = [UIFont systemFontOfSize:14];
    money.font = [UIFont systemFontOfSize:14];
    num.font = [UIFont systemFontOfSize:14];

    [headV addSubview:titleL];
    [headV addSubview:num];
    [headV addSubview:money];


    return headV;
}


// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableArray *dataArr = self.dataDic[@"data_list"];
    NSDictionary *mineDic = dataArr[indexPath.row];
    KeBiaoTongjiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"KeBiaoTongjiTableViewCell" forIndexPath:indexPath];
    cell.BanjiL.text = mineDic[@"coursename"];
    cell.numberOfLessonL.text = mineDic[@"group_nums"];
    cell.moneyOfLessonL.text = mineDic[@"group_fee"];
    cell.selectionStyle = 0;
    return cell;
}

-(void)Request
{
    [RequestManager RequstWithUrl:@"/index.php?g=App&m=TeacherIndex&a=statTeacher" parmDic:@{@"year":year,@"month":month} requestType:RequestGET Success:^(id data)
    {
        NSLog(@"mydata ===%@",data);

        if ([data isKindOfClass:[NSArray class]])
        {
            self.dataDic =@{@"total_fee":@"0.00",@"total_nums":@"0",@"data_list":@[]};
        }else
        {
            self.dataDic = data;
        }
        [self.mineListV reloadData];

    } error:^(NSError *error) {

    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
